package db;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class DBTable extends JTable implements ActionListener{
  ResultSetModel model;

  public DBTable(Connection c, String s){
    model=new ResultSetModel(c, s);
    setModel(model);
  }

  public void anfrage(String anfr){
    model.anfrage(anfr);
  }

  @Override
  public void actionPerformed(ActionEvent ev){
    anfrage(((JTextField)ev.getSource()).getText());
  }

  public static void main(String[] args){
    Connection conn;
    try{
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      conn=DriverManager.getConnection(
          "jdbc:mysql://gow.dyndns.info/world", "schule", "schulepass"
          );
    }
    catch (Exception e){
      conn=null;
      System.out.println("Fehler: "+e.toString());
    }
    JFrame jf=new JFrame("Datenbankanzeige");
    Container cont=jf.getContentPane();
    DBTable table=new DBTable(conn, "SELECT * FROM CountryLanguage");
    JScrollPane sp=new JScrollPane(table);
    cont.add(sp, BorderLayout.CENTER);
    JTextField jtf=new JTextField("SELECT * FROM CountryLanguage");
    cont.add(jtf, BorderLayout.NORTH);
    jtf.addActionListener(table);
    jf.pack();
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

/** Dieses TableModel holt sich seine Daten aus einem ResultSet von einem
 * Datenbankquery. Es ermittelt automatisch die Spaltenüberschriften
 * und stellt die Daten typgerecht dar.
 */
class ResultSetModel extends AbstractTableModel{
  Connection conn;
  Statement stmt;
  ResultSet resultset;
  int size;
  ResultSetMetaData rsmd;

  public ResultSetModel(Connection c, String anfr){
    conn=c;
    try{
      stmt=c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
          ResultSet.CONCUR_UPDATABLE);
    } catch(SQLException ex){
      System.out.println("kein Statement");
      stmt=null;
    }
    anfrage(anfr);
  }

  public void anfrage(String s){
    ResultSet res;
    try{
      res=stmt.executeQuery(s);
    } catch(SQLException ex){
      System.out.println("kein Resultset");
      res=null;
    }
    setResultSet(res);
  }

  public void setResultSet(ResultSet rs){
    try{
      if(resultset!=null) resultset.close();
      resultset=rs;
      rsmd=rs.getMetaData();
      rs.last();
      size=rs.getRow();
      rs.first();
    } catch(Exception ex){
      System.out.println("keine Daten");
      size=-1;
    }
    //System.out.println("Zeilen: "+getRowCount()+" Spalten:"+getColumnCount());
    fireTableStructureChanged();
  }

  @Override
  public int getRowCount(){
    return size;
  }

  @Override
  public int getColumnCount(){
    try{return rsmd.getColumnCount();}
    catch(SQLException ex){return -1;}
  }

  @Override
  public Object getValueAt(int row, int column){
    Object ret;
    try{
      resultset.absolute(row+1);
      ret=resultset.getObject(column+1);
      if(ret==null) ret="";
      return ret;
    } catch(SQLException ex){return "";}
  }

  @Override
  public Class getColumnClass(int col){
    //return String.class;
    return getValueAt(0, col).getClass();
  }

  @Override
  public String getColumnName(int col){
    try{return rsmd.getColumnLabel(col+1);}
    catch(SQLException ex){return super.getColumnName(col);}
  }

  @Override
  public boolean isCellEditable(int row, int col) {
    return col>1;
  }

  @Override
  public void setValueAt(Object value, int row, int col) {
    //data[row][col] = value;
    fireTableCellUpdated(row, col);
  }
}
